﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WonderfulSet
{
    public partial class fNewGame : Form
    {
        PictureBox[] masPB = new PictureBox[18];
        public fNewGame()
        {
            InitializeComponent();
            lCards.Visible = false;
            lSetDone.Visible = false;
            pictureBox1.Visible = false;
            pictureBox2.Visible = false;
            pictureBox3.Visible = false;
            masPB[0] = pictureBox4;
            masPB[1] = pictureBox5;
            masPB[2] = pictureBox6;
            masPB[3] = pictureBox7;
            masPB[4] = pictureBox8;
            masPB[5] = pictureBox9;
            masPB[6] = pictureBox10;
            masPB[7] = pictureBox11;
            masPB[8] = pictureBox12;
            masPB[9] = pictureBox13;
            masPB[10] = pictureBox14;
            masPB[11] = pictureBox15;
            masPB[12] = pictureBox1;
            masPB[13] = pictureBox2;
            masPB[14] = pictureBox3;
            masPB[15] = pictureBox16;
            masPB[16] = pictureBox17;
            masPB[17] = pictureBox18;
        }
        Card[] masCards = Methods.Forming();
        ArrayList masActivePB = new ArrayList();
        int ButClick = 0;
        int SetDone = 0;
        int InvisiblePB = 0;
        private void bAddCards_Click(object sender, EventArgs e)
        {
            int three = 3;
            for (int i = 0; ((i < 18)&&(three!=0)); i++)
            {
                if (masPB[i].Visible == false)
                {
                    if (A.Count != 0)
                    {
                        masPB[i].Visible = true;
                        three--;
                        InvisiblePB--;
                        masCards[(int)A[0]].Draw(masPB[i]);
                        A.RemoveAt(0);
                        lCards.Visible = true;
                        lCards.Text = A.Count.ToString();
                    }
                }
            }
            if (masPB[16].Visible == true)
                    this.Height = 679;
            if ((masPB[15].Visible == false) && (masPB[16].Visible == false) && (masPB[17].Visible == false))
                    this.Height = 585;
        }
        int sum = 0;
        ArrayList masActiveCards = new ArrayList();
        ArrayList A = new ArrayList();
        private void bStart_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            int iNumbersCount = 81;
            int[] aNumbers = new int[iNumbersCount];
            for (int j = 0; j < aNumbers.Length; j++)
                aNumbers[j] = j;


            for (int j = 0; j < 81; j++)
            {
                int iIndex = r.Next(iNumbersCount);
                A.Add(aNumbers[iIndex]);
                aNumbers[iIndex] = aNumbers[iNumbersCount - 1];
                iNumbersCount--;
            }
            for (int i = 11; i >=0 ; i--)
            {
                masPB[i].Visible = true;
                masCards[(int)A[i]].Draw(masPB[i]);//рисование
                A.RemoveAt(i);
            } 
            bStart.Visible = false;
            bAddCards.Visible = true;
            bMenu.Visible = true;
            //bHelp.Visible = true;
        }
        private void pictureBox5_Click(object sender, EventArgs e)
        {
            picB = pictureBox5;
            SetMethod();
        }

        PictureBox picB = new PictureBox();
        private void pictureBox4_Click(object sender, EventArgs e)
        {
            picB = pictureBox4;
            SetMethod();
        }
        

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            picB = pictureBox6;
            SetMethod();
        }
        

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            picB = pictureBox7;
            SetMethod();
        }
        

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            picB = pictureBox8;
            SetMethod();
        }
        

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            picB = pictureBox9;
            SetMethod();
        }
        

        private void pictureBox10_Click(object sender, EventArgs e)
        {
            picB = pictureBox10;
            SetMethod();
        }
        

        private void pictureBox11_Click(object sender, EventArgs e)
        {
            picB = pictureBox11;
            SetMethod();
        }
        

        private void pictureBox12_Click(object sender, EventArgs e)
        {
            picB = pictureBox12;
            SetMethod();
        }
        

        private void pictureBox13_Click(object sender, EventArgs e)
        {
            picB = pictureBox13;
            SetMethod();
        }
        

        private void pictureBox14_Click(object sender, EventArgs e)
        {
            picB = pictureBox14;
            SetMethod();
        }


        private void pictureBox15_Click(object sender, EventArgs e)
        {
            picB = pictureBox15;
            SetMethod();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            picB = pictureBox1;
            SetMethod();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            picB = pictureBox2;
            SetMethod();
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            picB = pictureBox3;
            SetMethod();
        }

        private void pictureBox16_Click(object sender, EventArgs e)
        {
            picB = pictureBox16;
            SetMethod();
        }

        private void pictureBox17_Click(object sender, EventArgs e)
        {
            picB = pictureBox17;
            SetMethod();
        }

        private void pictureBox18_Click(object sender, EventArgs e)
        {
            picB = pictureBox18;
            SetMethod();
        }

        private void bMenu_Click(object sender, EventArgs e)
        {
            Form1 F1 = new Form1();
            F1.Show();
            this.Hide();
        }




        public void SetMethod()
        {
            ActionPB(picB);
            if (masActiveCards.Count == 3)
            {

                Card c = (Card)masActiveCards[0];
                Card c1 = (Card)masActiveCards[1];
                Card c2 = (Card)masActiveCards[2];

                if ((Methods.IsSetColor(c, c1, c2) == "ng") || (Methods.IsSetNum(c, c1, c2) == "ng") || (Methods.IsSetShade(c, c1, c2) == "ng") ||
                    (Methods.IsSetShape(c, c1, c2) == "ng"))
                {
                    MessageBox.Show("Not Set", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    for (int i = 0; i < 3; i++)
                    {
                        PictureBox pb = (PictureBox)masActivePB[i];
                        pb.BorderStyle = BorderStyle.None;
                        pb.Invalidate();
                    }
                }
                else
                {
                    MessageBox.Show("Set", "Уведомление", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    if ((ButClick == 0) && (A.Count != 0))
                    {
                        for (int i = 0; i < 18; i++)
                        {
                            if (masPB[i].Visible == false)
                                InvisiblePB++;
                        }
                        if (InvisiblePB >= 6)
                        {

                            for (int i = 0; i < 3; i++)
                            {
                                PictureBox pb = (PictureBox)masActivePB[i];
                                pb.Visible = true;
                                masCards[(int)A[0]].Draw((PictureBox)masActivePB[i]);
                                A.RemoveAt(0);
                            }

                        }
                        else
                        {
                            for (int i = 0; i < 3; i++)
                            {
                                PictureBox pb = (PictureBox)masActivePB[i];
                                pb.Visible = false;
                            }
                        }
                        InvisiblePB = 0;
                    }

                    if ((ButClick != 0) || (A.Count == 0))
                    {
                        PictureBox pb = (PictureBox)masActivePB[0];
                        pb.Visible = false;
                        pb = (PictureBox)masActivePB[1];
                        pb.Visible = false;
                        pb = (PictureBox)masActivePB[2];
                        pb.Visible = false;
                        ButClick--;
                    }
                    SetDone++;
                    lSetDone.Text = SetDone.ToString();
                    lCards.Text = A.Count.ToString();
                    lSetDone.Visible = true;
                    lCards.Visible = true;
                    for (int i = 0; i < 3; i++)
                    {
                        PictureBox pb = (PictureBox)masActivePB[i];
                        pb.BorderStyle = BorderStyle.None;
                        //A.Remove(A[i]);
                        pb.Invalidate();
                    }
                }
                sum = 0;
                masActivePB.Clear();
                masActiveCards.Clear();
            }
        }
        

        private void ActionPB(PictureBox pb)
        {
            if (!(masActiveCards.Contains(pb.Tag)))
            {
                pb.BorderStyle = BorderStyle.FixedSingle;
                masActiveCards.Add(pb.Tag);
                masActivePB.Add(pb);
                sum++;
            }
            else
            {
                pb.BorderStyle = BorderStyle.None;
                masActiveCards.Remove(pb.Tag);
                masActivePB.Remove(pb);
                sum--;
            }
        }

    }
}
